package com.example.smartcity_poverty.ui.AllService.OA.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.OA.activity.OaShowMeTrueActivity
import kotlinx.android.synthetic.main.fragment_oa_show_me_ao_data.*
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject


class OaShowMeAoDataFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_oa_show_me_ao_data, container, false)
    }

    // 预约Id
    var oaShowIDDD: Int? = null
    var money: String? = null
    var oaShowTime: String? = null
    var oaShowType: String? = null
    var oaCratName: String? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (OaShowMeId == 10) {
            oaShowMeAoLayout.visibility = View.VISIBLE
            if (getToken().isNotEmpty()) {
                // 发起网络请求获取数据
                coroutine(Dispatchers.IO) {
                    val result = api.getOaShowMeAoData(getToken(), OaIdAAA)
                    activity?.runOnUiThread {
                        if (result.code == 200) {
                            result.data?.let {
                                oaShowMeAoTime.text = it.reserveTime
                                oaShowMeAoClass.text = it.categoryName

                                oaShowIDDD = it.id.toInt()
                                money = it.money.toString()
                                oaShowTime = it.reserveTime
                                oaShowType = it.type
                                oaCratName = it.categoryName
                            }
                        } else activity?.runOnUiThread { result.msg?.show() }
                    }
                }
            } else "请先登录！".show()

        } else oaShowMeAoLayout.visibility = View.GONE

        if (OaShowMeId == 20) {
            oaShowMeAoVipLayout.visibility = View.VISIBLE
        } else oaShowMeAoVipLayout.visibility = View.GONE

        // 点击预约
        oaShowMeAoOk.setOnClickListener {
            coroutine(Dispatchers.Main) {
                // 提交数据
                api.getOaShowMeAdd(getToken(), JSONObject().apply {
                    put("categoryId", oaShowIDDD)
                    put("patientName", oaShowName)
                    put("reserveTime", oaShowTime)
                    put("type", "普通号")
                }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                    if (this.code == "200") {
                        context?.startActivity(
                            Intent(
                                context,
                                OaShowMeTrueActivity::class.java
                            ).apply {
                                putExtra("time", oaShowTime)
                                putExtra("class1", oaCratName)
                                putExtra("class2", "普通号")
                            })
                    } else activity?.runOnUiThread { this.msg.show() }
                }
            }

        }
    }

    companion object {
        var OaShowMeId = 0
        var OaIdAAA = 0
        var oaShowName = ""
    }
}